package com.wuxiaofan.service;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.entity.HospitalSet;
import com.wuxiaofan.entity.custom.HospitalSetCustom;
import com.wuxiaofan.entity.model.HospitalSetMongo;
import com.wuxiaofan.mapper.HospitalSetMapper;
import com.wuxiaofan.repository.HospitalSetRepository;
import com.wuxiaofan.util.DateUtils;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.Resource;

import com.wuxiaofan.util.KeyWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class HospitalSetService {
    @Autowired
    private HospitalSetMapper hospitalSetMapper;
    @Resource
    private HospitalSetRepository hospitalSetRepository;
    public HospitalSet findById(String id){
        return hospitalSetMapper.selectById(id);
    }

    public HospitalSetCustom findCountByMap(Map paraMap){
        return hospitalSetMapper.findCountByMap(paraMap);
    }

    public HospitalSetCustom findGroupByMap(Map paraMap){
        return hospitalSetMapper.findGroupByMap(paraMap);
    }

    public List<HospitalSetCustom> findByMap(Map paraMap){
        return hospitalSetMapper.findByMap(paraMap);
    }

    public Page<HospitalSetCustom> findByMap(Map paraMap, Page page){
        return page.setRecords(hospitalSetMapper.findByMap(paraMap,page));
    }


    public void updateMapById(Map paraMap){
        paraMap.put("updateTime", DateUtils.getNowDate());
        hospitalSetMapper.updateMapById(paraMap);
    }
    public void delById(String id){
        hospitalSetMapper.deleteById(id);
    }

    public void batchRemove(List<String> ids){
        hospitalSetMapper.deleteBatchIds(ids);
    }

    public HospitalSetMongo getByHoscode(String hoscode){
        HospitalSetMongo hospitalSetMongo = hospitalSetRepository.getHospitalByHoscode(hoscode);
        return hospitalSetMongo;
    }

    public void saveOrUpdate(HospitalSet hospitalSet){
        if(StringUtils.isBlank(hospitalSet.getId())){
            hospitalSet.setId(KeyWorker.nextIdStr());
            hospitalSet.setCreateTime(DateUtils.getNowDateStr());
            hospitalSet.setUpdateTime(DateUtils.getNowDateStr());
            hospitalSetMapper.insert(hospitalSet);
        }else{
            hospitalSetMapper.updateById(hospitalSet);
        }

    }

    public void save(HospitalSetMongo hospitalSet){
        String sign = getSign(JSONObject.parseObject(JSONObject.toJSONString(hospitalSet),Map.class),
                hospitalSet.getHoscode());
        //判断是否存在
        HospitalSetMongo hospitalSetMongo = hospitalSetRepository.getHospitalByHoscode(hospitalSet.getHoscode());
        if(null != hospitalSetMongo) {
            hospitalSetRepository.save(hospitalSetMongo);
        } else {
            //0：未上线 1：已上线
            hospitalSet.setSign(sign);
            hospitalSetRepository.save(hospitalSet);
        }
    }
    /**
     * 请求数据获取签名
     * @param paramMap
     * @param signKey
     * @return
     */
    public static String getSign(Map<String, Object> paramMap, String signKey) {
        if(paramMap.containsKey("sign")) {
            paramMap.remove("sign");
        }
        TreeMap<String, Object> sorted = new TreeMap<>(paramMap);
        StringBuilder str = new StringBuilder();
        for (Map.Entry<String, Object> param : sorted.entrySet()) {
            str.append(param.getValue()).append("|");
        }
        str.append(signKey);
        log.info("加密前:{}",str.toString());
        String md5Str = SecureUtil.md5(str.toString());
        log.info("加密后:{}",md5Str);
        return md5Str;
    }

    /**
     * 签名校验
     * @param paramMap
     * @param signKey
     * @return
     */
    public static boolean isSignEquals(Map<String, Object> paramMap, String signKey) {
        String sign = (String)paramMap.get("sign");
        String md5Str = getSign(paramMap, signKey);
        if(!sign.equals(md5Str)) {
            return false;
        }
        return true;
    }

}
